package com.yunkeji.admin.controller.broker;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.secure.BCrypt;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.ObjectUtil;
import com.yunkeji.common.annotation.Log;
import com.yunkeji.common.constant.UserConstants;
import com.yunkeji.common.core.controller.BaseController;
import com.yunkeji.common.core.domain.PageQuery;
import com.yunkeji.common.core.domain.R;
import com.yunkeji.common.core.page.TableDataInfo;
import com.yunkeji.common.enums.BusinessType;
import com.yunkeji.common.excel.ExcelResult;
import com.yunkeji.common.helper.LoginHelper;
import com.yunkeji.common.utils.StreamUtils;
import com.yunkeji.common.utils.StringUtils;
import com.yunkeji.common.utils.poi.ExcelUtil;
import com.yunkeji.dao.domain.broker.BrokerUser;
import com.yunkeji.dao.domain.sys.SysDept;
import com.yunkeji.dao.domain.sys.SysPost;
import com.yunkeji.dao.domain.sys.SysRole;
import com.yunkeji.dao.domain.sys.SysUser;
import com.yunkeji.dao.vo.sys.SysUserExportVo;
import com.yunkeji.dao.vo.sys.SysUserImportVo;
import com.yunkeji.system.listener.SysUserImportListener;
import com.yunkeji.system.service.*;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 推广用户信息
 *
 * @author 雷擎宇
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/broker/user")
public class BrokerUserController extends BaseController {

    private final IBrokerUserService brokerUserService;

    /**
     * 获取用户列表
     */
    @SaCheckPermission("broker:user:list")
    @GetMapping("/list")
    public TableDataInfo<BrokerUser> list(BrokerUser user, PageQuery pageQuery) {
        return brokerUserService.selectPageUserList(user, pageQuery);
    }


    /**
     * 新增用户
     */
    @SaCheckPermission("broker:user:add")
    @Log(title = "新增推广用户", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@RequestBody BrokerUser user) {
        user.setPassword(BCrypt.hashpw(user.getPassword()));
        return toAjax(brokerUserService.insertUser(user));
    }

    /**
     * 修改用户
     */
    @SaCheckPermission("broker:user:edit")
    @Log(title = "修改推广用户", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@RequestBody BrokerUser user) {
        return toAjax(brokerUserService.updateUser(user));
    }

    /**
     * 删除
     */
    @SaCheckPermission("broker:user:delete")
    @Log(title = "删除推广用户", businessType = BusinessType.UPDATE)
    @DeleteMapping("/{userId}")
    public R<Void> delete(@PathVariable Long userId) {
        return toAjax(brokerUserService.deleteUserById(userId));
    }

    /**
     * 根据用户编号获取详细信息
     *
     * @param userId 用户ID
     */
    @GetMapping("/{userId}")
    public R<Map<String, Object>> getInfo(@PathVariable(value = "userId") Long userId) {
        Map<String, Object> ajax = new HashMap<>();
        BrokerUser sysUser = brokerUserService.selectUserById(userId);
        ajax.put("user", sysUser);
        return R.ok(ajax);
    }

}
